package com.leantech.android.plugin

import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.InputStreamReader


data class ProjectInfo(
    val appName: String, val versionName: String, val applicationId: String
) {
    fun versionCode(): Int {
        return versionName.replace(".", "").toIntOrNull() ?: 0
    }
}

fun getFlavors(configFile: File): Map<String, ProjectInfo> {
    try {
        if (!configFile.exists()) {
            error("${configFile.absolutePath} is not exists.")
        }
        val fileBufferedReader =
            BufferedReader(InputStreamReader(FileInputStream(configFile), Charsets.UTF_8))
        val gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()
        val type = TypeToken.getParameterized(
            Map::class.java, String::class.java, ProjectInfo::class.java
        ).type
        return gson.fromJson(fileBufferedReader, type)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return emptyMap()
}